package com.qianbao.qianbaobusiness.ui.login;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.config.Constant;
import com.baseandroid.utils.MoneyUtil;
import com.baseandroid.utils.TimeUtils;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.EInvoiceListBean;

import java.util.List;

/**
 * @author: Routee
 * @date 2018/6/12
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class EInvoiceListRvAdapter extends BaseRecycleViewAdapter<EInvoiceListBean.ItemsBean> {
    private ClickListener mListener;

    public EInvoiceListRvAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, final EInvoiceListBean.ItemsBean bean) {
        VH vh = (VH) holder;
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onItemClick(bean);
                }
            }
        });
        vh.mTvName.setText(bean.getRiseName());
        vh.mTvTime.setText(TimeUtils.getDate(bean.getInvoiceDate()
                ,Constant.DATE_YYYY_MM_DD_HH_MM_SS
                , Constant.DATE_YYYY_MM_DD));
        String money = MoneyUtil.parseMoney(bean.getInvoiceAmount());
        vh.mTvMoney.setText("开票金额：" + money + "元");
        switch (bean.getStatus()) {
            case "1":
                vh.mTvState.setText("开票成功");
                vh.mTvState.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
                break;
            case "2":
                vh.mTvState.setText("开票失败");
                vh.mTvState.setTextColor(Color.parseColor("#FFA800"));
                break;
            case "3":
                vh.mTvState.setText("已作废");
                vh.mTvState.setTextColor(Color.parseColor("#FFA800"));
                break;
            default:
                break;
        }
    }

    @Override
    protected VH onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_e_invoice_list, null);
        return new VH(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    public void resetItem(String invoiceId) {
        List<EInvoiceListBean.ItemsBean> data = getData();
        for (EInvoiceListBean.ItemsBean bean : data) {
            if (bean.getInvoiceId().equals(invoiceId)) {
                bean.setStatus("3");
                notifyDataSetChanged();
                return;
            }
        }
    }

    class VH extends RecyclerView.ViewHolder {

        TextView mTvName;
        TextView mTvTime;
        TextView mTvMoney;
        TextView mTvState;

        public VH(View itemView) {
            super(itemView);
            mTvName = itemView.findViewById(R.id.tv_name);
            mTvTime = itemView.findViewById(R.id.tv_time);
            mTvMoney = itemView.findViewById(R.id.tv_money);
            mTvState = itemView.findViewById(R.id.tv_state);
        }
    }

    public interface ClickListener {
        void onItemClick(EInvoiceListBean.ItemsBean bean);
    }

    public void setItemClickListener(ClickListener listener) {
        mListener = listener;
    }

}
